package com.gopay.solution.dao.impl;

import com.gopay.solution.domain.po.SsTransFeeDtl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 方案服务 手续费方案明细表
 * 
 * @SsAcctGainDtlDAOImpl.java
 * @author liujy
 * @2013-3-26 上午11:05:44 www.gopay.com.cn Inc.All rights reserved.
 */

@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository("ssTransFeeDtlDAO")
public class SsTransFeeDtlDAOImpl extends
        SolutionRuleDtlDAOImpl<SsTransFeeDtl, Serializable> {

    @Override
    public SsTransFeeDtl getByPk(Serializable id) {
        return find(SsTransFeeDtl.class, id);
    }

    @Override
    public List<SsTransFeeDtl> listBySolutionId(long solutionId) {
        String hql = "from SsTransFeeDtl dtl where dtl.solutionId = :SolutionId order by dtl.sq";
        
        Map params = new HashMap();
        params.put("SolutionId", solutionId);
        
        return findByHql(hql, params);
    }
    
    @Override
    public boolean delBySolutionId(long solutionId) {

        List<SsTransFeeDtl> dtls = listBySolutionId(solutionId);
        if (dtls != null && dtls.size() > 0) {
            deleteAll(dtls);
        }
        return true;
    }
    
    public List<SsTransFeeDtl> listBySoluIdAndIntTxnCd(long solutionId, String intTxnCd) {
        String hql = "from SsTransFeeDtl dtl where dtl.solutionId = :SolutionId and dtl.gopayIntTxnCd =:GopayIntTxnCd order by dtl.sq ";
        Map params = new HashMap();
        // add by zhaoxubing 20170801 复制方案的时候传入多种交易类型，需要in查询
        params.put("GopayIntTxnCd", intTxnCd);
        params.put("SolutionId", solutionId);
        if(StringUtils.contains(intTxnCd,",")){
            hql = "from SsTransFeeDtl dtl where dtl.solutionId = :SolutionId and dtl.gopayIntTxnCd in (:GopayIntTxnCd) order by dtl.sq ";
            params.put("GopayIntTxnCd", intTxnCd.split(","));
        }


        return findByHql(hql, params);
    }

    @Override
    public boolean delBySoluIdAndIntTxnCd(long solutionId, String intTxnCd) {
        List<SsTransFeeDtl> dtls = listBySoluIdAndIntTxnCd(solutionId, intTxnCd);
        if (dtls != null && dtls.size() > 0) {
            deleteAll(dtls);
        }
        return true;
    }
}
